const tool = require("../../utils/tool");
const config = require("../../config")

// pages/bg/bg.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showPage: false,
    tz_xts: false,
    tz_ycl: false,
    tz_myd: false,
    gg: undefined,
    user_searchInput: '',
    dimension_data: {},
    dimension_nameData: {},
    dimension_inputSF: '',
    dimension_inputXQ: '',
    dimension_inputLX: '',
    user_XQ: '',
    user_LX: '',
    user_showRPList: false,
    user_RPList: [],
    user_haveRP:false,
    user_haveREP:false,
    comList:[],
    serverDownloadImageURL:'',
    showSearchForm:false,
    tsSearchFormStatus:'不限制',
    tsSearchFormInput:'',
    tsSearchFormUserType:'巡查人',
    tsSearchFormXQ:'不限制',
    tsSearchFormLX:'不限制',
    tsSearchFormTSStart:'不限制',
    tsSearchFormTSEnd:'不限制',
    tsSearchFormTSStartDate:'不限制',
    tsSearchFormTSEndDate:'不限制',
    myCom:[],
    myComTotal:-1,
    comView:'简略',
    tsSearchFormPage:1,
    tsSearchFormSize:10,
    myComCurrentPage:'xj',
    myComSize:10,
    myComPage:1,
    mc:false,
    comSelected:[]
  },

  tsSearchFormUserTypeChange(e){
    e = parseInt(e.detail.value)
    let arr = ['巡查人','负责人','审核人']
    this.setData({
      tsSearchFormUserType:arr[e],

    })
  },

  async deleteSelected(){
    wx.showModal({
      title: '提示',
      content: '是否继续？',
      complete: async (res) => {
        if (res.cancel) {
          
        }
    
        if (res.confirm) {
          wx.showLoading({ mask:true,
            title: '请稍候',
          })
          let re = await tool.requestMethod("/com/batchDelete",{
            account:this.data.loginedUserInfo.account,
            ids:this.data.comSelected
          })
          wx.hideLoading()
          if(re.status==-2){
            tool.showToast('后端服务异常')
            return
          }
          wx.showLoading({ mask:true,
            title: '请稍候',
          })
          this.setData({
            comList:[]
          })
          await this.getComs()
          wx.hideLoading()
          this.setData({
            comSelected:[]
          })
        }
      }
    })
    
  },

  addComSelected(e){
    e = e.detail
    let re = this.data.comSelected
    re.push(e)
    this.setData({
      comSelected:re
    })
  },

  deleteComSelected(e){
    e = e.detail
    this.setData({
      comSelected:tool.arrDeleteItem(this.data.comSelected,e)
    })
  },

  disableMC(){
    this.setData({
      mc:false,
      comSelected:[]
    })
  },

  enableMC(){
    wx.vibrateShort({
      type: 'heavy',
    })
    if(this.data.mc)return
    this.setData({
      mc:true,
      comSelected:[]
    })
  },

  async getNext(){
    this.setData({
      myComPage:this.data.myComPage + 1
    })
    wx.showLoading({ mask:true,
      title: '请稍候',
    })
    await this.getMyComs()
    wx.hideLoading()
  },

  changeMyComPage(e){
    this.setData({
      myComCurrentPage:e.currentTarget.dataset.index,
      tsSearchFormPage:1,
    })
    this.myComPageTypeChanged()
  },

  changeComView(){
    wx.showActionSheet({
      itemList: ['正常','简略'],
      success:res=>{
        this.setData({
          comView:['正常','简略'][res.tapIndex]
        })
      }
    })
  },

  tsSearchFormTSStartChange(){
    let l = ['不限制','限制']
    wx.showActionSheet({
      itemList: l,
      success:(e)=>{
        e = e.tapIndex
        this.setData({
          tsSearchFormTSStart:l[e]
        })
      }
    })
  },

  tsSearchFormTSEndChange(){
    let l = ['不限制','限制']
    wx.showActionSheet({
      itemList: l,
      success:(e)=>{
        e = e.tapIndex
        this.setData({
          tsSearchFormTSEnd:l[e]
        })
      }
    })
  },

  tssfc(e){
    switch (e.currentTarget.dataset.type) {
      case '校区':
        let t = this.data.dimension_nameData[e.currentTarget.dataset.type][parseInt(e.detail.value)]
        this.setData({
          user_XQ:t
        })
        break;
      case '类别':
        let t1 = this.data.dimension_nameData[e.currentTarget.dataset.type][parseInt(e.detail.value)]
        this.setData({
          user_LX:t1
        })
        break;
    }
    this.user_rp_get()
  },

  tsSearchFormLXChange(){
    let l = ['不限制']
    for(let i=0;i<this.data.dimension_data['类别'].length;i++){
      l.push(this.data.dimension_data['类别'][i].name)
    }
    console.log(l)
    wx.showActionSheet({
      itemList: l,
      success:(e)=>{
        e = e.tapIndex
        this.setData({
          tsSearchFormLX:l[e]
        })
      },
      fail:(e)=>{
        console.log(e)
      }
    })
  },

  tsSearchFormTSStartDateChange(e){
    this.setData({
      tsSearchFormTSStartDate:e.detail.value
    })
  },

  tsSearchFormTSEndDateChange(e){
    this.setData({
      tsSearchFormTSEndDate:e.detail.value
    })
  },

  tsSearchFormXQChange(){
    let l = ['不限制']
    for(let i=0;i<this.data.dimension_data['校区'].length;i++){
      l.push(this.data.dimension_data['校区'][i].name)
    }
    wx.showActionSheet({
      itemList: l,
      success:(e)=>{
        e = e.tapIndex
        this.setData({
          tsSearchFormXQ:l[e]
        })
      }
    })
  },

  tsSearchFormStatusChange(){
    let l = ['不限制','待处理','待评价','已完成']
    wx.showActionSheet({
      itemList: l,
      success:(e)=>{
        e = e.tapIndex
        this.setData({
          tsSearchFormStatus:l[e]
        })
      }
    })
  },

  searchForm_wxmsInput(e){
    e = e.detail.value
    this.setData({
      tsSearchFormInput:e
    })
  },

  hideSearchForm(){
    this.setData({
      showSearchForm:false
    })
  },

  showSearchFormClick(){
    this.setData({
      showSearchForm:true,
    })
  },

  async user_rp_delete(e){
    e = e.currentTarget.dataset
    wx.showModal({
      title: '提示',
      content: '是否删除此' + (e.type=='rp'?'负责人':'审核人') + '？',
      complete: async (res) => {
        if (res.confirm) {
          wx.showLoading({ mask:true,
            title: '请稍候',
          })
          let re = await tool.requestMethod("/rp/delete",{
            XQ:this.data.user_XQ,
            LX:this.data.user_LX,
            phoneNumber:e.phonenumber,
            type:e.type
          })
          wx.hideLoading()
          if(re.status==-2){
            wx.showModal({
              title: '提示',
              content: '后端服务异常',
              showCancel: false
            })
            return
          }
          this.user_rp_get()
        }
      }
    })
  },

  async user_rp_add() {
    if (this.data.user_searchInput.length != 11) {
      wx.showModal({
        title: '提示',
        content: '电话号码格式有误',
        showCancel: false
      })
      return
    }
    wx.showActionSheet({
      itemList: ['设为负责人','设为审核人'],
      success:async res=>{
        wx.showLoading({ mask:true,
          title: '请稍候',
        })
        let re = await tool.requestMethod("/rp/add", {
          phoneNumber: this.data.user_searchInput,
          XQ: this.data.user_XQ,
          LX: this.data.user_LX,
          type:res.tapIndex==0?'rp':'rep'
        })
        wx.hideLoading()
        if (re.status == -2) {
          wx.showModal({
            title: '提示',
            content: '后端服务异常',
            showCancel: false
          })
          return
        }
        if(re.status==0){
          wx.showModal({
            title: '提示',
            content: re.content,
            showCancel: false
          })
          return
        }
        this.setData({
          user_searchInput:''
        })
        this.user_rp_get()
      }
    })
  },

  async user_rp_get() {
    if (this.data.user_LX == '' || this.data.user_XQ == '') {
      return
    }
    this.setData({
      user_showRPList: false
    })
    wx.showLoading({ mask:true,
      title: '请稍候',
    })
    let re = await tool.requestMethod("/rp/get", {
      XQ: this.data.user_XQ,
      LX: this.data.user_LX
    })
    wx.hideLoading()
    if (re.status == -2) {
      wx.showModal({
        title: '提示',
        content: '后端服务异常',
        showCancel: false
      })
      return
    }
    let tt1 = false
    let tt2 = false
    for(let i=0;i<re.results.length;i++){
      if(re.results[i].type=='rp')tt1 = true
      if(re.results[i].type=='rep')tt2 = true
    }
    this.setData({
      user_RPList: re.results,
      user_showRPList: true,
      user_haveREP:tt2,
      user_haveRP:tt1
    })
  },

  user_XQ_click() {
    let re = []
    for (let i = 0; i < this.data.dimension_data['校区'].length; i++) {
      re.push(this.data.dimension_data['校区'][i].name)
    }
    wx.showActionSheet({
      itemList: re,
      success: (e) => {
        this.setData({
          user_XQ: re[e.tapIndex]
        })
        this.user_rp_get()
      }
    })
  },

  user_LX_click() {
    let re = []
    for (let i = 0; i < this.data.dimension_data['类别'].length; i++) {
      re.push(this.data.dimension_data['类别'][i].name)
    }
    wx.showActionSheet({
      itemList: re,
      success: (e) => {
        this.setData({
          user_LX: re[e.tapIndex]
        })
        this.user_rp_get()
      }
    })
  },

  dimension_inputSF(e) {
    this.setData({
      dimension_inputSF: e.detail.value
    })
  },

  dimension_inputXQ(e) {
    this.setData({
      dimension_inputXQ: e.detail.value
    })
  },

  dimension_inputLX(e) {
    this.setData({
      dimension_inputLX: e.detail.value
    })
  },

  async dimension_addClick(e) {
    let data = e.currentTarget.dataset
    switch (data.type) {
      case '身份':
        if (this.data.dimension_inputSF == '') {
          wx.showToast({
            title: '请输入身份名称',
            icon: "none"
          })
          return
        }
        break;
      case '校区':
        if (this.data.dimension_inputXQ == '') {
          wx.showToast({
            title: '请输入校区名称',
            icon: "none"
          })
          return
        }
        break;
      case '类别':
        if (this.data.dimension_inputLX == '') {
          wx.showToast({
            title: '请输入类别名称',
            icon: "none"
          })
          return
        }
        break;
    }
    let input = ''
    switch (data.type) {
      case '身份':
        input = this.data.dimension_inputSF
        break;
      case '校区':
        input = this.data.dimension_inputXQ
        break;
      case '类别':
        input = this.data.dimension_inputLX
        break;
    }
    if(input=='全部' || input=='不限制' || input=='无限制'){
      tool.showToast('不能设置此名称')
      return
    }
    wx.showLoading({ mask:true,
      title: '请稍候',
    })
    let re = await tool.requestMethod("/dimension/add", {
      type: data.type,
      name: input
    })
    wx.hideLoading()
    if (re.status == -2) {
      wx.showModal({
        title: '提示',
        content: '后端服务异常',
        showCancel: false
      })
      return
    }
    wx.showLoading({ mask:true,
      title: '请稍候',
    })
    re = await tool.requestMethod("/dimension/get", {})
    wx.hideLoading()
    if (re.status == -2) {
      wx.showModal({
        title: '提示',
        content: '后端服务异常',
        showCancel: false
      })
      return
    }
    this.setData({
      dimension_data: re.results
    })
    switch (data.type) {
      case '身份':
        this.setData({
          dimension_inputSF: ''
        })
        break;
      case '校区':
        this.setData({
          dimension_inputXQ: ''
        })
        break;
      case '类别':
        this.setData({
          dimension_inputLX: ''
        })
        break;
    }
  },

  async dimension_deleteClick(e) {
    wx.showModal({
      title: '提示',
      content: '对应的负责人也会被删除，此操作不可恢复，是否继续？',
      complete: async (res) => {
        if (res.confirm) {
          let data = e.currentTarget.dataset
          wx.showLoading({ mask:true,
            title: '请稍候',
          })
          let re = await tool.requestMethod("/dimension/delete", {
            type: data.type,
            name: data.name
          })
          wx.hideLoading()
          if (re.status == -2) {
            wx.showModal({
              title: '提示',
              content: '后端服务异常',
              showCancel: false
            })
            return
          }
          wx.showLoading({ mask:true,
            title: '请稍候',
          })
          re = await tool.requestMethod("/dimension/get", {})
          wx.hideLoading()
          if (re.status == -2) {
            wx.showModal({
              title: '提示',
              content: '后端服务异常',
              showCancel: false
            })
            return
          }
          this.setData({
            dimension_data: re.results
          })
        }
      }
    })

  },

  user_searchInputInput(e) {
    this.setData({
      user_searchInput: e.detail.value
    })
  },

  user_searchInputClear() {
    this.setData({
      user_searchInput: ''
    })
  },

  async cb(item){
    item = item.currentTarget.dataset.item
    wx.showLoading({ mask:true,
      title: '请稍候',
    })
    let re = await tool.requestMethod("/com/hasten",{
      id:item.id
    })
    wx.hideLoading()
    if(re.status==-2){
      wx.showModal({
        title: '提示',
        content: '后端服务异常',
        showCancel: false
      })
      return
    }
    if(re.status==0){
      wx.showModal({
        title: '提示',
        content: re.content,
        showCancel: false
      })
      return
    }
    wx.showModal({
      title: '提示',
      content: '催办成功',
      showCancel: false
    })
  },

  async makePhoneCall(e){
    e = e.currentTarget.dataset.account
    wx.showLoading({ mask:true,
      title: '请稍候',
    })
    let re = await tool.requestMethod("/user/phoneNumber",{
      account:e
    })
    wx.hideLoading()
    if(re.status==-2){
      wx.showModal({
        title: '提示',
        content: '后端服务异常',
        showCancel: false
      })
      return
    }
    if(re.results.length!=11){
      wx.showModal({
        title: '提示',
        content: '当前帐号未设置电话号码',
        showCancel: false
      })
      return
    }
    wx.makePhoneCall({
      phoneNumber: re.results,
    })
  },

  showComItemMenu(e){
    e = e.currentTarget.dataset.item
    let menu = []
    if(e.account==this.data.loginedUserInfo.account){
      if(e.account2=='' && e.content3.length==0){
        menu = ['详情','撤回']
      }else{
        menu = ['详情']
      }
    }else{
      menu = ['详情']
    }
    wx.showActionSheet({
      itemList: menu,
      success:async res=>{
        res = res.tapIndex
        switch (res) {
          case 0:
            this.showComDetail(e.id)
            break;
          case 1:
            wx.showModal({
              title: '提示',
              content: '是否撤回此巡查？',
              complete: async (res) => {
                if (res.cancel) {
                  
                }
            
                if (res.confirm) {
                  wx.showLoading({ mask:true,
                    title: '请稍候',
                  })
                  let re = await tool.requestMethod("/com/withdraw",{
                    id:e.id
                  })
                  wx.hideLoading()
                  if(re.status==-2){
                    wx.showModal({
                      title: '提示',
                      content: '后端服务异常',
                      showCancel: false
                    })
                    return
                  }
                  if(re.status==0){
                    wx.showModal({
                      title: '提示',
                      content: re.content,
                      showCancel: false
                    })
                    return
                  }
                  if(this.data.mgType=='与我相关的巡查'){
                    let reWDXC = await tool.requestMethod("/com/get",{
                      account:this.data.loginedUserInfo.account,
                      type:'U'
                    })
                    if(reWDXC.status==-2){
                      wx.showToast({
                        title: '后端服务异常',
                        icon: 'none'
                      })
                      wx.navigateBack()
                      return
                    }
                    this.setData({
                      myCom:reWDXC.results.all
                    })
                  }else{
                    this.getComs()
                  }
                  
                }
              }
            })
          break
        }
      }
    })
  },

  showImageViewer(e){
    for(let i=0;i<e.currentTarget.dataset.arr.length;i++){
      e.currentTarget.dataset.arr[i] = config.serverURL + '/file/download/' + e.currentTarget.dataset.arr[i]
    }
    console.log(e)
    wx.previewImage({
      urls: e.currentTarget.dataset.arr,
      current: config.serverURL + '/file/download/' + e.currentTarget.dataset.current
    })
  },

  showComDetail(id){
    if(id.currentTarget!=undefined){
      id = id.currentTarget.dataset.id
    }
    wx.navigateTo({
      url: '/pages/comDetail/conDetail?data=' + JSON.stringify({
        id:id,
        statusBarHeight:this.data.statusBarHeight,
        contentHeight:this.data.contentHeight,
        loginedUserInfo:this.data.loginedUserInfo
      }),
    })
  },

  tsSearchFormChange(e){
    switch (e.currentTarget.dataset.type) {
      case '校区':
        this.setData({
          tsSearchFormXQ:this.data.dimension_nameData['校区2'][parseInt(e.detail.value)]
        })
        break;
      case '类别':
        this.setData({
          tsSearchFormLX:this.data.dimension_nameData['类别2'][parseInt(e.detail.value)]
        })
        break;
    }
  },

  mpk(e){
    tool.showComPhoneNumberList(this.data.statusBarHeight,this.data.contentHeight,this.data.loginedUserInfo,e.currentTarget.dataset.id)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    tool.setThemeColor(this)
    this.setData({
      sysImagURLPrefix:wx.getStorageSync('sysImagURLPrefix')
    })
    this.setData({
      serverDownloadImageURL:config.serverURL + '/file/download'
    })
    options = JSON.parse(options.data)
    this.setData({
      statusBarHeight: (parseInt(options.statusBarHeight.split('px')[0])).toString() + 'px',
      loginedUserInfo: options.loginedUserInfo,
      contentHeight: (parseInt(options.contentHeight.split('px')[0]) + 3).toString() + 'px',
      mgType: options.mgType
    })
    if(options.account2!=undefined){
      switch (options.type) {
        case 'dcl':
          options.type = '待处理'
          break;
        case 'dpj':
          options.type = '待评价'
          break;
        case 'ywc':
          options.type = '已完成'
          break;
        default:
          options.type = '无限制'
          break;
      }
      this.setData({
        tsSearchFormStatus:options.type,
        tsSearchFormInput:options.account2,
      })
    }
    wx.showLoading({ mask:true,
      title: '正在获取',
    })
    let re2 = await tool.requestMethod("/dimension/get", {})
        if (re2.status == -2) {
          wx.showModal({
            title: '提示',
            content: '后端服务异常',
            showCancel: false
          })
        }
        let t = {
          身份:[],
          校区:[],
          类别:[],
          校区2:['不限制'],
          类别2:['不限制'],
        }
        for(let i=0;i<re2.results.身份.length;i++){
          t.身份.push(re2.results.身份[i].name)
        }
        for(let i=0;i<re2.results.校区.length;i++){
          t.校区.push(re2.results.校区[i].name)
          t.校区2.push(re2.results.校区[i].name)
        }
        for(let i=0;i<re2.results.类别.length;i++){
          t.类别.push(re2.results.类别[i].name)
          t.类别2.push(re2.results.类别[i].name)
        }
        this.setData({
          dimension_data: re2.results,
          dimension_nameData: t,
          dimension_MSData: [t.校区,t.类别]
        })
    switch (this.data.mgType) {
      case '功能开关':
        let re = await tool.requestMethod("/bg/settingList", {})
        if (re.status == -2) {
          wx.showModal({
            title: '提示',
            content: '后端服务异常',
            showCancel: false
          })
          return
        }
        if (re.status == 0) {
          await tool.requestMethod("/setting/addOrUpdate", {
            keyName: 'xcsh',
            value: 'false'
          })
          this.setData({
            tz_xts:false,
            tz_ycl:false,
            tz_myd:false,
            tz_sh:false,
            tz_xcsh:false,
            tz_zc:false,
            tz_zcyz:false,
          })
        } else {
          re = re.results
          this.setData({
            tz_xts:re.tz_xts,
            tz_ycl:re.tz_ycl,
            tz_myd:re.tz_myd,
            tz_sh:re.tz_sh,
            tz_xcsh:re.tz_xcsh,
            tz_zc:re.tz_zc,
            tz_zcyz:re.tz_zcyz,
          })
        }
        break;
      case '用户管理':
        this.getUserInfo(options.account)
        break
      case '多维度管理设置':
        let re1 = await tool.requestMethod("/dimension/get", {})
        if (re1.status == -2) {
          wx.showModal({
            title: '提示',
            content: '后端服务异常',
            showCancel: false
          })
          break
        }
        this.setData({
          dimension_data: re1.results
        })
        break
      case '管理巡查':
        
        let re21 = await tool.requestMethod("/dimension/get", {})
        if (re21.status == -2) {
          wx.showModal({
            title: '提示',
            content: '后端服务异常',
            showCancel: false
          })
          break
        }
        this.setData({
          dimension_data: re21.results
        })
        await this.getComs()
        this.setData({
          tsSearchFormTSStartDate:(new Date()).getFullYear().toString() + '-' + ((new Date()).getMonth() + 1).toString() + '-' + (new Date()).getDate().toString(),
          tsSearchFormTSEndDate:(new Date()).getFullYear().toString() + '-' + ((new Date()).getMonth() + 1).toString() + '-' + (new Date()).getDate().toString(),
        })
      break
      case '与我相关的巡查':
        if(options.type){
          this.setData({
            myComCurrentPage:options.type
          })
        }
        await this.getMyComs(true)
      break
    }
    wx.hideLoading()
    this.setData({
      showPage: true
    })
  },

  async myComPageTypeChanged(){
    wx.showLoading({ mask:true,
      title: '请稍候',
    })
    this.setData({
      myComTotal:-1,
      myCom:[]
    })
    await this.getMyComs()
    wx.hideLoading()
  },

  async getMyComs(exit = false){
    let re22 = await tool.requestMethod("/com/my",{
      type:this.data.myComCurrentPage,
      account:this.data.loginedUserInfo.account,
      size:this.data.myComSize,
      page:this.data.myComPage
    })
    if(re22.status==-2){
      wx.showModal({
        title: '提示',
        content: '后端服务异常',
        showCancel: false,
        complete: (res) => {
          if(exit){
            wx.navigateBack()
          }
        }
      })
      return
    }
    let re = this.data.myCom
    for(let i=0;i<re22.results.data.length;i++){
      re.push(re22.results.data[i])
    }
    console.log(re22)
    this.setData({
      myCom:re,
      myComTotal:re22.results.total
    })
  },

  userMakePhoneCall(){
    wx.makePhoneCall({
      phoneNumber: this.data.userInfo.phoneNumber,
    })
  },

  async userFreeze(){
    wx.showModal({
      title: '提示',
      content: this.data.userInfo.type=='U'?'是否冻结此账号？冻结后用户无法登录此账号':'是否取消冻结此账号？',
      complete: async (res) => {
        if (res.cancel) {
          
        }
    
        if (res.confirm) {
          wx.showLoading({ mask:true,
            title: '请稍候',
          })
          let re = await tool.requestMethod("/user/freeze",{
            account:this.data.userInfo.account,
            op:this.data.userInfo.type=='U'?'do':'undo'
          })
          wx.hideLoading()
          if(re.status==-2){
            wx.showToast({
              title: '后端服务异常',
            })
            return
          }
          this.getUserInfo(this.data.userInfo.account)
        }
      }
    })
    
  },

  async userSetSAdmin(e){
    e = e.detail.value
    wx.showLoading({ mask:true,
      title: '请稍候',
    })
    let re = await tool.requestMethod("/sadmin",{
      op:e?'add':'delete',
      phoneNumber:this.data.userInfo.phoneNumber
    })
    wx.hideLoading()
    if(re.status==-2){
      wx.showModal({
        title: '提示',
        content: '后端服务异常',
        showCancel: false
      })
      return
    }
    this.getUserInfo(this.data.userInfo.account)
  },

  async userDelete(){
    wx.showModal({
      title: '提示',
      content: '是否删除此用户？',
      complete: async (res) => {
        if (res.cancel) {
          
        }
    
        if (res.confirm) {
          wx.showLoading({ mask:true,
            title: '请稍候',
          })
          let re = await tool.requestMethod("/user/delete",{
            account:this.data.userInfo.account,
            phoneNumber:this.data.userInfo.phoneNumber
          })
          wx.hideLoading()
          if(re.status==-2){
            wx.showToast({
              title: '后端服务异常',
              icon: 'none'
            })
            return
          }
          wx.showToast({
            title: '删除成功',
            icon: 'none'
          })
          wx.navigateBack()
        }
      }
    })
  },

  async userResetPassword(){
    wx.showModal({
      title: '提示',
      content: '此用户的登录密码将重置为 12345678',
      complete: async (res) => {
        if (res.cancel) {
          
        }
    
        if (res.confirm) {
          wx.showLoading({ mask:true,
            title: '请稍候',
          })
          let re = await tool.requestMethod("/user/resetPassword",{
            account:this.data.userInfo.account
          })
          wx.hideLoading()
          if(re.status==-2){
            wx.showToast({
              title: '后端服务异常',
              icon: 'none'
            })
            return
          }
          wx.showToast({
            title: '重置成功',
            icon: 'none'
          })
        }
      }
    })
  },

  async userUnbindWechat(){
    wx.showModal({
      title: '提示',
      content: '是否解绑微信？解绑后此用户再次打开小程序时需要重新登录。',
      complete: async (res) => {
        if (res.cancel) {
          
        }
    
        if (res.confirm) {
          wx.showLoading({ mask:true,
            title: '请稍候',
          })
          let re = await tool.requestMethod("/user/unbindWeChat",{
            account:this.data.userInfo.account
          })
          wx.hideLoading()
          if(re.status==-2){
            wx.showToast({
              title: '后端服务异常',
              icon: 'none'
            })
            return
          }
          wx.showToast({
            title: '解绑成功',
            icon: 'none'
          })
        }
      }
    })
  },

  userDeleteRP(e){
    e = e.currentTarget.dataset
    console.log(e)
    wx.showModal({
      title: '提示',
      content: '是否删除此权限？',
      complete: async (res) => {
        if (res.cancel) {
          
        }
    
        if (res.confirm) {
          wx.showLoading({ mask:true,
            title: '请稍候',
          })
          let re = await tool.requestMethod("/rp/delete",{
            type:'rp',
            LX:e.lx,
            XQ:e.xq,
            phoneNumber:this.data.userInfo.phoneNumber
          })
          wx.hideLoading()
          if(re.status==-2){
            wx.showModal({
              title: '提示',
              content: '后端服务异常',
              showCancel: false
            })
            return
          }
          this.getUserInfo(this.data.userInfo.account)
        }
      }
    })
  },

  async userAddRP(e){
    e = e.detail.value
    if(e==undefined)return
    wx.showLoading({ mask:true,
      title: '请稍候',
    })
    let re = await tool.requestMethod("/rp/add",{
      phoneNumber:this.data.userInfo.phoneNumber,
      type:'rp',
      LX:this.data.dimension_MSData[1][e[1]],
      XQ:this.data.dimension_MSData[0][e[0]]
    })
    wx.hideLoading()
    if(re.status==-2){
      wx.showModal({
        title: '提示',
        content: '后端服务异常',
        showCancel: false
      })
      return
    }
    if(re.status==0){
      wx.showModal({
        title: '提示',
        content: re.content,
        showCancel: false
      })
      return
    }
    this.getUserInfo(this.data.userInfo.account)
  },

  userDeleteREP(e){
    e = e.currentTarget.dataset
    wx.showModal({
      title: '提示',
      content: '是否删除此权限？',
      complete: async (res) => {
        if (res.cancel) {
          
        }
    
        if (res.confirm) {
          wx.showLoading({ mask:true,
            title: '请稍候',
          })
          let re = await tool.requestMethod("/rp/delete",{
            type:'rep',
            LX:e.lx,
            XQ:e.xq,
            phoneNumber:this.data.userInfo.phoneNumber
          })
          wx.hideLoading()
          if(re.status==-2){
            wx.showModal({
              title: '提示',
              content: '后端服务异常',
              showCancel: false
            })
            return
          }
          this.getUserInfo(this.data.userInfo.account)
        }
      }
    })
  },

  async userAddREP(e){
    e = e.detail.value
    if(e==undefined)return
    wx.showLoading({ mask:true,
      title: '请稍候',
    })
    let re = await tool.requestMethod("/rp/add",{
      phoneNumber:this.data.userInfo.phoneNumber,
      type:'rep',
      LX:this.data.dimension_MSData[1][e[1]],
      XQ:this.data.dimension_MSData[0][e[0]]
    })
    wx.hideLoading()
    if(re.status==-2){
      wx.showModal({
        title: '提示',
        content: '后端服务异常',
        showCancel: false
      })
      return
    }
    if(re.status==0){
      wx.showModal({
        title: '提示',
        content: re.content,
        showCancel: false
      })
      return
    }
    this.getUserInfo(this.data.userInfo.account)
  },

  async getUserInfo(account){
    let re333 = await tool.requestMethod("/bg/singleUserInfo",{
      account:account
    })
    if(re333.status==-2){
      wx.showModal({
        title: '提示',
        content: '后端服务异常',
        showCancel: false
      })
      wx.navigateBack()
      return
    }
    if(re333.status==0){
      wx.showModal({
        title: '提示',
        content: re333.content,
        showCancel: false
      })
      wx.navigateBack()
      return
    }
    re333 = re333.results
    this.setData({
      userInfo:re333
    })
  },

  async getComsClick(){
    wx.showLoading({ mask:true,
      title: '请稍候',
    })
    this.setData({
      showSearchForm:false,
      comList:[],
      tsSearchFormPage:1
    })
    await this.getComs()
    wx.hideLoading()
  },

  async getNextComs(){
    this.setData({
      tsSearchFormPage:this.data.tsSearchFormPage + 1,
      tsSearchGetting:true
    })
    await this.getComs()
    this.setData({
      tsSearchGetting:false
    })
  },

  async getComs(clear = false){
    let re4 = await tool.requestMethod("/com/get",{
      type:'admin',
      tsSearchFormStatus:this.data.tsSearchFormStatus,
      tsSearchFormInput:this.data.tsSearchFormInput,
      tsSearchFormXQ:this.data.tsSearchFormXQ,
      tsSearchFormLX:this.data.tsSearchFormLX,
      tsSearchFormTSStart:this.data.tsSearchFormTSStart,
      tsSearchFormTSEnd:this.data.tsSearchFormTSEnd,
      tsSearchFormTSStartDate:this.data.tsSearchFormTSStartDate,
      tsSearchFormTSEndDate:this.data.tsSearchFormTSEndDate,
      tsSearchFormUserType:this.data.tsSearchFormUserType,
      page:this.data.tsSearchFormPage,
      size:this.data.tsSearchFormSize
    })
    if(re4.status==-2){
      wx.showModal({
        title: '提示',
        content: '后端服务异常',
        showCancel: false
      })
      wx.navigateBack()
    }
    if(clear){
      this.setData({
        comListTotal:0,
        comList:[]
      })
    }
    let comListCopy1 = this.data.comList
    for(let i=0;i<re4.results.data.length;i++){
      comListCopy1.push(re4.results.data[i])
    }
    this.setData({
      comList:comListCopy1,
      comListTotal:re4.results.total
    })
  },

  getPhoneNumber(e) {
    console.log(e)
    wx.login({
      success: (res) => {
        tool.requestMethod("/phoneNumber", {
          code: res.code,
          encryptedData: e.detail.encryptedData,
          iv: e.detail.iv
        })
      },

    })
  },

  async tz_xts_change(e) {
    let re = await this.changeSetting('sendMessage', [e.detail.value, this.data.tz_ycl])
    if (re == true) {
      this.setData({
        tz_xts: e.detail.value
      })
    }
  },

  async tz_ycl_change(e) {
    let re = await this.changeSetting('sendMessage', [this.data.tz_xts, e.detail.value])
    if (re == true) {
      this.setData({
        tz_ycl: e.detail.value
      })
    }
  },

  async tz_myd_change(e) {
    let re = await this.changeSetting('myd', e.detail.value.toString())
    if (re == true) {
      this.setData({
        tz_myd: e.detail.value
      })
    }
    if(this.data.tz_myd==false){
      await tool.requestMethod("/setAllUnSatComToSated",{})
    }
  },

  async tz_xcsh_change(e) {
    let re = await this.changeSetting('xcsh', e.detail.value.toString())
    if (re == true) {
      this.setData({
        tz_xcsh: e.detail.value
      })
    }
  },

  async tz_sh_change(e) {
    let re = await this.changeSetting('sh', e.detail.value.toString())
    if (re == true) {
      this.setData({
        tz_sh: e.detail.value
      })
    }
  },

  async tz_zc_change(e) {
    let re = await this.changeSetting('zc', e.detail.value.toString())
    if (re == true) {
      this.setData({
        tz_zc: e.detail.value
      })
    }
  },

  async tz_zcyz_change(e) {
    let re = await this.changeSetting('zcyz', e.detail.value.toString())
    if (re == true) {
      this.setData({
        tz_zcyz: e.detail.value
      })
    }
  },

  async changeSetting(keyName, value) {
    wx.showLoading({ mask:true,
      title: '正在保存设置',
    })
    let re = await tool.requestMethod("/setting/addOrUpdate", {
      keyName: keyName,
      value: value
    })
    wx.hideLoading()
    if (re.status == -2) {
      wx.showModal({
        title: '提示',
        content: '后端服务异常',
        showCancel: false
      })
      return false
    }
    return true
  },

  back() {
    wx.navigateBack()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  async onPullDownRefresh() {
    if(this.data.mgType=='管理巡查'){
      this.setData({
        tsSearchFormPage:1
      })
      await this.getComs(true)
    }
    if(this.data.mgType=='与我相关的巡查'){
      this.setData({
        myComPage:1,
        myCom:[],
        myComTotal:0
      })
      await this.getMyComs()
    }
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})